package myapp.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper{
	 
	//The Android's default system path of your application database.
	//private static String DB_PATH = "/data/data/example.dbdemo/databases/";
	 

	public static final int DATABASE_VERSION = 1;
	
	public static final String dbName="demoDB";
	public static final String categoryTable="CATEGORY";
	public static final String colCategoryID="ID";
	public static final String colCategoryName="Name";
	public static final int colCategoryIDIndex=0;
	public static final int colCategoryNameIndex=1;
	
	public static final String itemTable="ITEM";
	public static final String colItemID="ID";
	public static final String colItemID_CATEGORY="ID_CATEGORY";
	public static final String colItemName="Name";
	public static final String colItemLong="Long";
	public static final String colItemLat="Lat";
	public static final String colItemAddress="Address";
	public static final String colItemPhone="Phone";
	public static final String colItemDescription="Description";
	public static final int colItemIDIndex=0;
	public static final int colItemID_CATEGORYIndex=1;
	public static final int colItemNameIndex=2;
	public static final int colItemLongIndex=3;
	public static final int colItemLatIndex=4;
	public static final int colItemAddressIndex=5;
	public static final int colItemPhoneIndex=6;
	public static final int colItemDescriptionindex=7;
	
	
	static final String viewEmps="ViewEmps";
	

	 
	/**
	  * Constructor
	  * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
	  * @param context
	  */
	public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE "+ categoryTable +" ("+ colCategoryID + " INTEGER PRIMARY KEY AUTOINCREMENT , "+ 
				colCategoryName+ " TEXT)");
		  
		db.execSQL("CREATE TABLE "+ itemTable +"("+ colItemID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+
				colItemID_CATEGORY + " INTEGER, " +
		        colItemName +" TEXT, "+
				colItemLong +" NUMERIC, "+
		        colItemLat + " NUMERIC, " +
				colItemAddress + " TEXT, " +
		        colItemPhone + " TEXT, " +
				colItemDescription + " TEXT, " +
		        "FOREIGN KEY (" + colItemID + ") REFERENCES " + categoryTable + " ("+ colCategoryID + "));");

	}
	 
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    Log.w("TaskDBAdapter", "Upgrading from version " + 
                oldVersion + " to " +
                newVersion + ", which will destroy all old data");

		// Drop the old table.
		db.execSQL("DROP TABLE IF EXISTS " + dbName);
		// Create a new one.
		onCreate(db);
	 
	}	
	
}
